import React from 'react';
import { Button, Divider, Modal, Space, Table, Tag, message } from 'antd';
import { deltrila } from '@/api1/trial/index'
import { useNavigate } from 'react-router-dom';
const { Column, ColumnGroup } = Table;

interface SearchtableProps {
  total: number;
  dataSource: DataType[];
  onPageChange: (current: number, pageSize?: number) => void;
  reloadData: () => void; // 新增重新加载数据的函数
}



const handleStatusChange = (orderId: any, reloadData: () => void) => { // 传入重新加载数据的函数

  const navigate = useNavigate();


navigate('/dashboard/addproducts')


  Modal.confirm({
    title: '确认删除',
    content: '您确定要删除这条记录吗？',
    onOk: () => {
      // 调用后端接口删除记录
      console.log('调用后端接口删除记录:', { ids: [orderId] });
      let ids = []
      ids.push(orderId)
      deltrila([orderId]).then((res: any) => {
        // if (res.code == 200) {
        message.success('删除成功！');
        reloadData(); // 删除成功后重新加载数据
        // } else {
        //   message.error('删除失败！');
        // }
      });
    },
    onCancel: () => {
      console.log('取消删除');
    },
  });

  // Add your logic to handle status change here
  // console.log(`Changing status of order ${orderId} to ${newStatus}`);
};


const del = (orderId: any, reloadData: () => void) => { // 传入重新加载数据的函数

  Modal.confirm({
    title: '确认删除',
    content: '您确定要删除这条记录吗？',
    onOk: () => {
      // 调用后端接口删除记录
      console.log('调用后端接口删除记录:', { ids: [orderId] });
      let ids = []
      ids.push(orderId)
      deltrila([orderId]).then((res: any) => {
        // if (res.code == 200) {
        message.success('删除成功！');
        reloadData(); // 删除成功后重新加载数据
        // } else {
        //   message.error('删除失败！');
        // }
      });
    },
    onCancel: () => {
      console.log('取消删除');
    },
  });

  // Add your logic to handle status change here
  // console.log(`Changing status of order ${orderId} to ${newStatus}`);
};

export const Searchtable: React.FC<SearchtableProps> = ({ dataSource, onPageChange, total, reloadData }) => ( // 接收并传递重新加载数据的函数
  <Table bordered={true} pagination={{ total }} dataSource={dataSource} onChange={(pagination) => onPageChange(pagination.current || 1, pagination.pageSize)}>
    <Column title="商品名称" dataIndex="storeName" key="storeName" />
    {/* <Column title="活动商品标题" dataIndex="title" key="title" /> */}
    <Column title="创建时间" dataIndex="createTime" key="createTime" />
    {/* <Column title="注册时间" dataIndex="price" key="price" /> */}
    {/* <Column title="订单号" dataIndex="orderNumber" key="orderNumber" />
    <Column title="总价" dataIndex="actualTotal" key="actualTotal" />
    <Column title="订单状态" dataIndex="status" key="status" /> */}
    <Column
      title="操作"
      dataIndex="tags"
      key="tags"
      render={(tags, record: any) => (
        <div className="space-x-4">
          {/* <Button type="primary" onClick={() => handleStatusChange(record, 'newStatus')}>
            添加消息
          </Button> */}


          <Button type="primary" onClick={() => handleStatusChange(record.id, reloadData)}> {/* 传入重新加载数据的函数 */}
            修改
          </Button>

          <Button type="primary" onClick={() => del(record.id, reloadData)}> {/* 传入重新加载数据的函数 */}
            删除
          </Button>

        </div>

      )}
    />,
  </Table>
);
